package at.main.gui;

import at.main.android.CanvasJ2ME;
import at.main.android.Graphics;
import at.main.util.AllImage;
import at.main.util.AllSound;
import at.main.util.ConfigPanel;
import at.main.util.ManagerFont;

public class MyCanvas extends CanvasJ2ME implements Runnable {
	public static final int k_JamoPage = 0;

	public static final int k_SoundPage = 1;

	public static final int k_SplashPage = 2;

	public static final int k_MenuPage = 3;

	public static final int k_PlayGame = 4;

	public static final int k_ContineGame = 5;

	public static final int k_OptionGame = 6;

	public static final int k_HelpGame = 7;

	public static final int k_AboutGame = 8;

	public static final int k_ExitGame = 9;

	public static final int k_MenuPlaying = 10;

	public static int iCurrentPage = 0;

	public static int iHeight, iWidth;

	public static boolean isRunning = false;

	public static boolean isPlaySound = false;

	public static boolean isInterrupted = false;

	public static int iTranslateX = 0;

	public static int iTranslateY = 0;

	public static boolean keyPressed[] = { false, false, false, false, false,
			false, false, false, false, false, false, false, false, false };

	public static boolean[] keyHold = new boolean[14];

	public static int iStack = 0;

	public static boolean isMoto;

	public boolean[] keyRepeated = new boolean[21];

	public static int iX;

	public static int iY;

	public static boolean isPressDown;

	public static boolean isPressUp;

	public static MyCanvas instanMyCanvas;

	public static int scr;

	public Thread t = null;

	// public game gamesrceen;

	public JamoPage jamoPage = null;

	public SoundPage soundPage = null;

	public SplashPage splashPage = null;

	public PlayGame playGame = null;

	public MenuPage menuPage = null;

	public OptionPage optionPage = null;

	public HelpPage helpPage = null;

	public AboutPage aboutPage = null;

	public ExitGamePage exitGamePage = null;

	public ContinePage continePage = null;

	public MenuPlaying menuPlaying = null;

	// public ManagerFont font;

	public MyCanvas() {
		this.setFullScreenMode(true);
		MyCanvas.iWidth = this.getWidth();
		MyCanvas.iHeight = this.getHeight();
		initTypeScreen();
		isMoto = (getKeyCode(FIRE) == -20);
		AllImage.createImage();
		// Khoi tao cac trang
		jamoPage = new JamoPage();
		iCurrentPage = 0;
		instanMyCanvas = this;
	}

	// Get Type Screen
	public void initTypeScreen() {
		if (MyCanvas.iWidth < 200 /* && MyCanvas.iHeight== 220 */)
			ConfigPanel.iTypeScreen = 0;
		else
			// if (MyCanvas.iWidth >= 200 /* && MyCanvas.iHeight == 320 */)
			ConfigPanel.iTypeScreen = 1;
	}

	public static void clearInput1(int index) {
		for (int i = 0; i < 14; i++) {
			if (i != index) {
				keyPressed[i] = false;
				keyHold[i] = false;
			}
		}
		isPressDown = false;
		isPressUp = false;
	}

	public static void clearInput() {
		for (int i = 0; i < 14; i++) {
			keyPressed[i] = false;
			keyHold[i] = false;
		}
		isPressDown = false;
		isPressUp = false;

	}

	public void keyPressed(int keycode) {
		// clearInput();
		if (isMoto) {
			switch (getGameAction(keycode)) {
			case UP:
				keyHold[2] = true;
				keyPressed[2] = true;			
				clearInput1(2);
				return;
			case DOWN:
				keyHold[8] = true;
				keyPressed[8] = true;
				clearInput1(8);
				return;
			case LEFT:
				keyHold[4] = true;
				keyPressed[4] = true;
				clearInput1(4);
				return;
			case RIGHT:
				keyHold[6] = true;
				keyPressed[6] = true;
				clearInput1(6);
				return;
			case FIRE:
				keyHold[5] = true;
				keyPressed[5] = true;
				// clearInput1(5);
				return;
			}
			if (keycode == -21) // Soft 1
				keycode = -6;
			else if (keycode == -22) // Soft 2
				keycode = -7;
		}

		switch (keycode) {
		case 48:
		case 49:
		case 51:
		case 55:
		case 57:
			keyHold[keycode - 48] = true;
			keyPressed[keycode - 48] = true;
			return;
		case 50:
		case 52:
			// case 53:
		case 54:
		case 56:
			keyHold[keycode - 48] = true;
			keyPressed[keycode - 48] = true;
			clearInput1(keycode - 48);
			return; // 0 -> 9
		case 42:
			keyHold[10] = true;
			return; // Key [*]
		case 35:
			keyHold[11] = true;
			return; // Key [#]
		case -6: // Right
			keyHold[12] = true;
			keyPressed[12] = true;
			clearInput1(12);
			return; // Soft1
		case -7: // Left
			keyHold[13] = true;
			keyPressed[13] = true;
			clearInput1(13);
			return; // Soft2
		case -5:
			keyHold[5] = true;
			keyPressed[5] = true;
			// clearInput1(5);
			return; // [i]
		}

		switch (getGameAction(keycode)) {
		case UP:
			keyHold[2] = true;
			keyPressed[2] = true;
			clearInput1(2);
			return;
		case DOWN:
			keyHold[8] = true;
			keyPressed[8] = true;
			clearInput1(8);
			return;
		case LEFT:
			keyHold[4] = true;
			keyPressed[4] = true;
			clearInput1(4);
			return;
		case RIGHT:
			keyHold[6] = true;
			keyPressed[6] = true;
			clearInput1(6);
			return;
		case FIRE:
			keyHold[5] = true;
			keyPressed[5] = true;
			// clearInput1(5);
			return;
		}
	}

	protected void pointerPressed(int x, int y) {
		iX = x;
		iY = y;
		isPressDown = true;
		isPressUp = false;
		// System.out.println("pointerPressed");
	}

	protected void pointerReleased(int x, int y) {
		iX = x;
		iY = y;
		isPressDown = false;
		isPressUp = true;
		// System.out.println("pointerReleased");
	}

	protected void hideNotify() {
		isRunning = false;
		isInterrupted = true;
		//t = null;
	}

	protected void showNotify() {
		isRunning = true;
		if (t == null) {
			t = new Thread(this);
			t.start();
		}
	}

	/*
	 * public void MoveMap(Graphics g) { g.translate(-iTranslateX,
	 * -iTranslateY); }
	 */
	// paint chinh cua mycanvas
	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		if (isInterrupted) {
			g.setColor(135, 206, 250);
			g.fillRect(0, 0, getWidth(), getHeight());
			g.setColor(255, 0, 0);
			g.drawString("Vui lòng chờ...", getWidth() / 3, getHeight() - 50,
					Graphics.TOP | Graphics.LEFT);
		} else {
			switch (iCurrentPage) {
			case k_JamoPage:
				jamoPage.paint(g);
				break;
			case k_SoundPage:
				if (soundPage == null)
					soundPage = new SoundPage();
				soundPage.paint(g);
				break;
			case k_SplashPage:
				if (splashPage == null)
					splashPage = new SplashPage();
				splashPage.paint(g);
				break;
			case k_MenuPage:
				if (menuPage == null)
					menuPage = new MenuPage();
				menuPage.paint(g);
				break;
			case k_PlayGame:
				if (playGame == null)
					playGame = new PlayGame();
				playGame.paint(g);
				break;
			case k_ContineGame:
				break;
			case k_OptionGame:
				if (optionPage == null)
					optionPage = new OptionPage();
				optionPage.paint(g);
				break;
			case k_HelpGame:
				if (helpPage == null)
					helpPage = new HelpPage();
				helpPage.paint(g);
				break;
			case k_AboutGame:
				if (aboutPage == null)
					aboutPage = new AboutPage();
				aboutPage.paint(g);
				break;
			case k_ExitGame:
				if (exitGamePage == null)
					exitGamePage = new ExitGamePage();
				exitGamePage.paint(g);
				break;
			case k_MenuPlaying:
				if (menuPlaying == null) {
					menuPlaying = new MenuPlaying();
				}
				menuPlaying.paint(g);
			default:
				break;
			}
		}
		g.setColor(0xFFFFFF);
		g.fillRect(getWidth() + iTranslateX - 60, getHeight() + iTranslateY
				- 15, 25, 12);
		ManagerFont.drawString(g, String.valueOf(fps), getWidth() + iTranslateX
				- 60, getHeight() + iTranslateY - 15, 0, 1);
		// if (fps > 500)
		// System.out.println(">>>>>>>>>>>> fps" + fps);
	}

	public static long Timer() {
		return System.currentTimeMillis();
	}

	long lTimeDraw = 0;

	public int fps = 0;

	final int k_MAX_FPS = 24;

	public void run() {
		// TODO Auto-generated method stub
		while (isRunning) {
			long l = System.currentTimeMillis();
			if (isInterrupted) {
				repaint();
				serviceRepaints();
				while (System.currentTimeMillis() - l < 1000) {
					Thread.yield();
				}
				isInterrupted = !isInterrupted;
			} else {
				switch (iCurrentPage) {
				case k_JamoPage:
					jamoPage.update();
					break;
				case k_SoundPage:
					if (soundPage == null)
						soundPage = new SoundPage();
					soundPage.update();
					break;
				case k_SplashPage:
					if (splashPage == null)
						splashPage = new SplashPage();
					splashPage.update();
					break;
				case k_MenuPage:
					if (menuPage == null)
						menuPage = new MenuPage();
					if (playGame != null)
						playGame = null;
					menuPage.update();
					break;
				case k_PlayGame:
					if (playGame == null) {
						playGame = new PlayGame();
					}
					// if(playGame.isTryAgain) //Choi lai
					// playGame = new PlayGame(true);
					playGame.update();
					break;
				case k_ContineGame:
					if (continePage == null)
						continePage = new ContinePage();
					break;
				case k_OptionGame:
					if (optionPage == null)
						optionPage = new OptionPage();
					optionPage.update();
					break;
				case k_HelpGame:
					if (helpPage == null)
						helpPage = new HelpPage();
					helpPage.update();
					break;
				case k_AboutGame:
					if (aboutPage == null)
						aboutPage = new AboutPage();
					aboutPage.update();
					break;
				case k_ExitGame:
					if (exitGamePage == null)
						exitGamePage = new ExitGamePage();
					exitGamePage.update();
					break;
				case k_MenuPlaying:
					if (menuPlaying == null) {
						menuPlaying = new MenuPlaying();
					}
					AllSound.Stop();
					menuPlaying.update();
				default:
					break;
				}
				repaint();
				serviceRepaints();
				lTimeDraw = System.currentTimeMillis() - l;
				lTimeDraw = lTimeDraw == 0 ? 1 : lTimeDraw;
				fps = (int) (1000 / lTimeDraw);
				int iTimeMaxframe = (int) 1000 / k_MAX_FPS;
				if (lTimeDraw < iTimeMaxframe) {
					while (System.currentTimeMillis() - l < iTimeMaxframe) {
						Thread.yield();
					}
				}
				// while (System.currentTimeMillis() - l < 50) {
				// Thread.yield();
				// }
			}

		}
	}
}